package com.ricchieri.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;

import org.springframework.security.core.context.SecurityContextHolder;

import com.ricchieri.common.CustomAuthenticationToken;

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@Table(name="cancelationMotive")
public class CancelationMotive extends BaseEntity {
	private static final long serialVersionUID = -5793821596209263405L;
	
	@Column(name="cancelationDate", nullable=false)	
	private Date cancelationDate;

	@Column(name="cancelationDescription", nullable=false, length=255)
	private String cancelationDescription;
	
	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "operator", nullable = false)	
	private User operator;
	
	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "cancelationType", nullable = false)
	private CancelationType cancelationType;
	
	public Date getCancelationDate() {
		return cancelationDate;
	}
	
	public void setCancelationDate(Date cancelationDate) {
		this.cancelationDate = cancelationDate;
	}
	
	public CancelationType getCancelationType() {
		return cancelationType;
	}
	
	public void setCancelationType(CancelationType cancelationType) {
		this.cancelationType = cancelationType;
	}
	
	public String getCancelationDescription() {
		return cancelationDescription;
	}
	
	public void setCancelationDescription(String cancelationDescription) {
		this.cancelationDescription = cancelationDescription;
	}

	public User getOperator() {
		return operator;
	}

	public void setOperator(User operator) {
		this.operator = operator;
	}
	
	@PrePersist
	public void obtainCurrentOperator() {
		User currentUser = ((CustomAuthenticationToken)SecurityContextHolder.getContext().getAuthentication()).getUser();
		this.setOperator(currentUser);
	}
	
}